package com.cloud.jdkTest;

import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class TestStreamSorted {

    @Data
    static class User {
        private String name;
        private Integer age;
        private Integer height;

        public User(String name, Integer age, Integer height) {
            this.name = name;
            this.age = age;
            this.height = height;
        }

        public String toString() {
            String info = String.format("%s\t\t%s\t\t%s\t\t\t", this.name, this.age.toString(), this.height.toString());
            return info;
        }
    }

    public static void sorted() {

        List<Integer> list = Arrays.asList(5, 4, 3, 2, 7, 1, 6, 0, 9, 8);
        List<Integer> newList = list.stream().map(x -> x + 1).collect(Collectors.toList());
        List<Integer> ascList = newList.stream().sorted().collect(Collectors.toList());
        List<Integer> descList1 = newList.stream().sorted(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        }).collect(Collectors.toList());

        List<Integer> descList2 = newList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

        System.out.println("原始数组:" + list);
        System.out.println("自增一后:" + newList);
        System.out.println("升序数组:" + ascList);
        System.out.println("降序数组1:" + descList1);
        System.out.println("降序数组2:" + descList2);

        System.out.println();

        List<User> personList = new ArrayList<>();
        personList.add(new User("张三", 16, 181));
        personList.add(new User("李四", 17, 180));
        personList.add(new User("王五", 16, 179));
        personList.add(new User("张明", 18, 177));
        personList.add(new User("李威", 16, 190));
        personList.add(new User("王鹏", 15, 188));
        personList.add(new User("王莉", 17, 177));

        //年龄升序，相同年龄身高升序
        List<User> ascPList = personList.stream()
                .sorted(Comparator.comparingInt(User::getAge).thenComparing(User::getHeight))
                .collect(Collectors.toList());
        //年龄降序，相同年龄身高降序
        List<User> descPList = personList.stream()
                .sorted(Comparator.comparingInt(User::getAge).reversed().thenComparing(User::getHeight).reversed())
                .collect(Collectors.toList());

        printStudents(ascPList);
        printStudents(descPList);

    }

    public static void printStudents(List<User> userList) {
        System.out.println("【姓名】\t【年龄】\t【身高】");
        System.out.println("-----------------------");
        userList.forEach(s -> System.out.println(s.toString()));
        System.out.println(" ");
    }


    public static void main(String[] args) {
        sorted();
    }

}


